package com.burst.reptile.modular.monitor.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.burst.reptile.common.constant.Constants;
import com.burst.reptile.common.utils.ToolUtil;
import com.burst.reptile.framework.aspectj.lang.annotation.Log;
import com.burst.reptile.framework.aspectj.lang.enums.BusinessType;
import com.burst.reptile.framework.redis.RedisCache;
import com.burst.reptile.framework.security.LoginUser;
import com.burst.reptile.framework.web.controller.BaseController;
import com.burst.reptile.framework.web.result.Result;
import com.burst.reptile.modular.monitor.entity.SysUserOnline;
import com.burst.reptile.modular.system.service.ISysUserOnlineService;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;

/**
 * 在线用户监控
 * @author wujin
 * @date: 2020年6月8日 下午2:18:36
 */
@RestController
@RequestMapping("/monitor/online")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysUserOnlineController extends BaseController {
	
	private final ISysUserOnlineService userOnlineService;

	private final RedisCache redisCache;

	/**
	 * 获取在线用户列表
	 * @param ipaddr
	 * @param userName
	 * @return
	 */
	@PreAuthorize("@ss.hasPermi('monitor:online:list')")
	@GetMapping("/list")
	public Result<Object> list(String ipaddr, String userName) {
		Collection<String> keys = redisCache.keys(Constants.LOGIN_TOKEN_KEY + "*");
		List<SysUserOnline> userOnlineList = new ArrayList<>();
		for (String key : keys) {
			Object cacheObject = redisCache.getCacheObject(key);
			if(ToolUtil.isEmpty(cacheObject)) {
				continue;
			}
			LoginUser user = new LoginUser();
			BeanUtil.copyProperties(cacheObject, user);
			if (StrUtil.isNotEmpty(ipaddr) && StrUtil.isNotEmpty(userName)) {
				if (StrUtil.equals(ipaddr, user.getIpaddr()) && StrUtil.equals(userName, user.getUsername())) {
					userOnlineList.add(userOnlineService.selectOnlineByInfo(ipaddr, userName, user));
				}
			} else if (StrUtil.isNotEmpty(ipaddr)) {
				if (StrUtil.equals(ipaddr, user.getIpaddr())) {
					userOnlineList.add(userOnlineService.selectOnlineByIpaddr(ipaddr, user));
				}
			} else if (StrUtil.isNotEmpty(userName) && ToolUtil.isNotEmpty(user.getUser())) {
				if (StrUtil.equals(userName, user.getUsername())) {
					userOnlineList.add(userOnlineService.selectOnlineByUserName(userName, user));
				}
			} else {
				userOnlineList.add(userOnlineService.loginUserToUserOnline(user));
			}
		}
		Collections.reverse(userOnlineList);
		userOnlineList.removeAll(Collections.singleton(null));
		return Result.success(userOnlineList);
	}

	/**
	 * 强退用户
	 */
	@PreAuthorize("@ss.hasPermi('monitor:online:forceLogout')")
	@Log(title = "在线用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/exit/{tokenId}")
	public Result<Object> forceLogout(@PathVariable String tokenId) {
		redisCache.deleteObject(Constants.LOGIN_TOKEN_KEY + tokenId);
		return SUCCESS;
	}
}
